package sms_interface;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

/**
 * 
 * @author aayushjain
 * Contains various date related functions i.e conversion of one format to another.
 * Subtracting some time from the date. 
 */
public class Date {

	
	
	/**
	 * Used for Subtracting some time from the date and time.
	 * @param val Contains Date and time. 
	 * @param time Time to be subtracted from the variable val.
	 * @param uni Units of the time to be subtracted. 
	 * @return String which contains the Date and Time with the subtracted time value.
	 * @throws Exception
	 */
	public static String subdate(String val,String time, String uni) throws Exception
	{
		java.util.Date date = new java.util.Date();
		DateFormat formatter ;
        formatter = new SimpleDateFormat("M.dd.yy H:m:s");
        date = (java.util.Date)formatter.parse(val);
        java.sql.Date d = new java.sql.Date(date.getTime());
        String arr5[]=d.toString().split("-");
        int year=Integer.parseInt(arr5[0]);
        int month=Integer.parseInt(arr5[1]);
        int day=Integer.parseInt(arr5[2]);
        
        GregorianCalendar gc =new GregorianCalendar(year,month-1,day,date.getHours(),date.getMinutes(),date.getSeconds());
        //System.out.println(year+" "+month+" "+day);
        if( uni.equalsIgnoreCase("min"))
        {
        
        gc.add(java.util.GregorianCalendar.MINUTE, -1*Integer.parseInt(time));
        }
        else if( uni.equalsIgnoreCase("sec"))
        {
            gc.add(java.util.GregorianCalendar.SECOND, -1*Integer.parseInt(time));
        }
        int fyear=gc.get(java.util.GregorianCalendar.YEAR);
        int fmon=gc.get(java.util.GregorianCalendar.MONTH)+1;
        int fday=gc.get(java.util.GregorianCalendar.DAY_OF_MONTH);
        int fhr=gc.get(java.util.GregorianCalendar.HOUR_OF_DAY);
        int fmin=gc.get(java.util.GregorianCalendar.MINUTE);
        int fsec=gc.get(java.util.GregorianCalendar.SECOND);
        
        
        return(fyear+"-"+fmon+"-"+fday+":"+fhr+"-"+fmin+"-"+fsec);
        
	}
	/**
	 * Conversion from one date format to another.
	 * @param val Contains Date and time of the format "Month.Date.Year Hours:Minutes:Seconds". 
	 * @return Date and Time of the format "Month-Date-Year:Hours-Minutes-Seconds"
	 * @throws Exception
	 */
	public static String strtodate(String val) throws Exception
	{
		java.util.Date date = new java.util.Date();
			DateFormat formatter ;
	        formatter = new SimpleDateFormat("M.dd.yy H:m:s");
	        date = (java.util.Date)formatter.parse(val);
	      //System.out.println(date);
	       
		java.sql.Date d = new java.sql.Date(date.getTime());
		return (d+":"+date.getHours()+"-"+date.getMinutes()+"-"+date.getSeconds());
	}
	
	/**
	 * Testing the two functions in the class: strtodate, subdate. Refer to their docs for instructions.
	 * @param args
	 * @throws Exception
	 */
	static public void main(String[] args) throws Exception{
		System.out.println(strtodate("10.25.11 15:52:33"));
		System.out.println(subdate("10.25.11 15:52:33","5","sec"));
		
	}
}
